/*
 * 文件名:CSharedMemory.cpp
 *
 * 	 功能描述:
 *  创建日期: 2010-12-25
 *  版本Ver0.01
 *  作者: huanying2000
 *  版权:copyright 2010 by the Camellia project 
 *  free software under the terms of the LGPL 2.1
 */

#include "CSharedMemory.hpp"

template <typename T>
CSharedMemory<T>::CSharedMemory(key_t shmkey)
{
    key = shmkey;
}

template <typename T>
CSharedMemory<T>::~CSharedMemory()
{
    cout << "~CShareMemory()\n";
}

template <typename T>
void CSharedMemory<T>::initShm()
{
    if ((shmid = shmget(key, sizeof(T), IPC_CREAT | 0600)) == -1)
    {
        cerr << "initShm[shmget] error!\n";
        exit(EXIT_FAILURE);
    }

    if ((pMsg = (T *)shmat(shmid, NULL, 0)) == (T *)-1)
    {
        cerr << "attachShm error!\n";
        exit(EXIT_FAILURE);
    }

    if (shmctl(shmid, IPC_RMID, &shmemds) == -1)
    {
        cerr << "initShm[shmctl] error!\n";
        exit(EXIT_FAILURE);
    }
}

template <typename T>
void CSharedMemory<T>::rmShm()
{

    if (shmdt((void *)pMsg) == -1)
    {
        cerr << "rmShm error!\n";
        exit(EXIT_FAILURE);
    }
}
